package com.sys.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.sys.dao.DictItemMapper;
import com.sys.dao.DictMapper;
import com.sys.domain.Dict;
import com.sys.domain.DictExample;
import com.sys.domain.DictExample.Criteria;
import com.sys.domain.DictItem;
import com.sys.domain.DictItemExample;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;
import com.sys.service.DictItemService;
import com.sys.service.DictService;

@Service("dictService")
public class DictServiceImpl implements DictService {
	
	@Resource
	private DictMapper dictMapper;
	
	@Resource
	private DictItemService dictItemService;
	
	@Resource
	private DictItemMapper dictItemMapper;

	@Override
	public RequestPageVo<Dict> queryDictList(PageParam param,String dictName,String dictCode) {
		RequestPageVo<Dict> pageVo=new RequestPageVo<Dict>();
		
		DictExample example=new DictExample();
		Criteria criteria=example.createCriteria();
		
		if(!StringUtils.isEmpty(dictName)){
			criteria.andDictNameLike("%"+dictName+"%");
		}
		if(!StringUtils.isEmpty(dictCode)){
			criteria.andDictCodeEqualTo(dictCode);
		}
		
		int count=dictMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<Dict> dictList=dictMapper.selectByExample(example);//字典项列表
			
			for(Iterator<Dict> Iter=dictList.iterator();Iter.hasNext();){//每个字典项下的值
				Dict dict=Iter.next();
				List<DictItem> dictItemList=dictItemService.queryDictItemByDictCode(dict.getDictCode());
				dict.setDictItemList(dictItemList);
			}
			
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			
			pageVo.setRows(dictList);
		}
		
		return pageVo;
	}
	
	@Override
	public RequestResultVo<Dict> deleteDict(Integer id){
		RequestResultVo<Dict> result = new RequestResultVo<Dict>();
		
		try {
			Dict dict=dictMapper.selectByPrimaryKey(id);
			String code=dict.getDictCode();//删除字典项下的字典值
			if(!StringUtils.isEmpty(code)){
				DictItemExample example=new DictItemExample();
				com.sys.domain.DictItemExample.Criteria criteria =example.createCriteria();
				criteria.andDictCodeEqualTo(code);
				dictItemMapper.deleteByExample(example);
			}
			
			int count = dictMapper.deleteByPrimaryKey(id);
			if(count>0){
				result.setSuccess(true);
			}else{
				result.setSuccess(false);
				result.setErrorMessage("删除失败!");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败!");
		}
		
		return result;
	}

	@Override
	public RequestResultVo<Dict> addDict(Dict dict) {
		RequestResultVo<Dict> result=new RequestResultVo<Dict>();
		try {
			String code=dict.getDictCode();//判断字典编码是否已存在
			DictExample example=new DictExample();
			Criteria criteria=example.createCriteria();
			criteria.andDictCodeEqualTo(code);
			List<Dict> list=dictMapper.selectByExample(example);
			if(list.size()>0){
				result.setSuccess(false);
				result.setErrorMessage("添加失败，字典编码不能重复!");
				return result;
			}else{
				dictMapper.insertSelective(dict);
				result.setSuccess(true);
			}
			
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败!");
		}
		return result;
	}

	@Override
	public RequestResultVo<Dict> updateDict(Dict dict) {
		RequestResultVo<Dict> result=new RequestResultVo<Dict>();
		try {
			dictMapper.updateByPrimaryKeySelective(dict);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("更新失败!");
		}
		return result;
	}

}
